'use strict';
app.service('transactionAnalysisService',['$http','$q','utilService','$cookies','$filter',function($http,$q,utilService,$cookies,$filter){
    //查询交易分析统计数据
    this.findTransactionAnalysis = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'transaction/analysis',
            params:{
                analysisType:$scope.analysis.analysisType,
                date:$scope.analysis.date,
                startTime:$scope.analysis.startTime,
                endTime:$scope.analysis.endTime
            }
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    }

    //导出交易分析详细数据
    this.exportData = function($scope){
        $http({
            method:'POST',
            url:$scope.app.domain + 'report/transaction/analysis',
            data:{
                exportType:$scope.data.exportType,
                sessionId:$cookies.sessionId
            },
            responseType: "blob",
            headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function(data){
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var fileName ="transaction"+ (new Date()).getTime()+'.xls';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    }

    //导出交易分析统计数据
    this.exportTransactionData = function($scope){
        $http({
            method:'POST',
            url:$scope.app.domain + 'report/transaction/all/analysis',
            data:{
                analysisType:$scope.analysis.analysisType,
                date:$scope.analysis.date,
                startTime:$scope.analysis.startTime,
                endTime:$scope.analysis.endTime,
                sessionId:$cookies.sessionId
            },
            responseType: "blob",
            headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function(data){
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var fileName ="transactionStatistical"+ (new Date()).getTime()+'.xls';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    }
}])